package leetcode.editor.cn;

import java.util.HashMap;

public class P397IntegerReplacement {
    public static void main(String[] args) {
        Solution solution = new P397IntegerReplacement().new Solution();
        System.out.println(solution.integerReplacement(8));

    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        HashMap<Integer, Integer> map = new HashMap<>();

        public int integerReplacement(int n) {
            if (n == 1) {
                return 0;
            }
            if (map.containsKey(n)) {
                return map.get(n);
            } else {
                if (n % 2 == 0) {
                    map.put(n, integerReplacement(n / 2) + 1);
                } else {
                    map.put(n, Math.min(integerReplacement(n / 2) + 2, integerReplacement(n / 2 + 1) + 2));
                }
            }
            return map.get(n);
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}